

use "$path\Intermediary Data\localshock_dataforReg_sp95_v2.dta",clear



drop if prix_ht==.
drop  j j3
xtset id daten
capture drop d_gasoliner
gen d_gasoliner=ln(gasoline_rotterdam_euro)-ln(l1.gasoline_rotterdam_euro)

drop gasoline_rotterdam_euro  dprix1 
*** Step 1 ***

gen t=1
bysort id: egen nobs_stations=sum(t)

keep if nobs_stations>500


gen minus_number_obs=-nobs_stations
sort minus_number_obs id


gen test=(local_shock10==.)
bys id: egen st=sum(test)

gen test2=1
bys id: egen st2=sum(test2)
drop if (st2==st)
gen tt=st2-st

drop if tt<40
drop t test st test2 st2 tt

**** Step 2 : Iterate ****


egen id_select=group(minus_number_obs id)
drop minus_number_obs
drop if id_select==.


drop nobs_stations idb id_pdv
sum id_select, d
save "$path\Intermediary Data\data_forestimation_temp_sp95.dta", replace



forvalues iter=50 (50) 8050 {
local iterm=`iter'-50
clear
use "$path\Intermediary Data\data_forestimation_temp_sp95.dta", clear
keep if id_select>`iterm' & id_select<=`iter'

qui sum id_select, d
local rmax=r(max)
local rmin=r(min)


forvalue j = `rmin' (1) `rmax'  {

preserve
qui keep if id_select==`j'
capture drop num_obs
gen num_obs=_n
*




foreach i of numlist 0/40{
capture drop  f_prix_ht

qui bysort id (daten):  gen f_prix_ht=log(prix_ht[_n+`i'])-log(prix_ht[_n-1])

qui reg f_prix_ht d_gasoliner local_shock10 m_dprix l.d_gasoliner l2.d_gasoliner l3.d_gasoliner l4.d_gasoliner l5.d_gasoliner ///
		l.local_shock10  l2.local_shock10 l3.local_shock10 l4.local_shock10 l5.local_shock10 ///
	l.m_dprix  l2.m_dprix l3.m_dprix l4.m_dprix l5.m_dprix

qui gen coeff1_reg_`i'=_b[d_gasoliner]
qui gen coeff2_reg_`i'=_b[local_shock10]

capture drop  f_prix_ht

}
quie duplicates drop id, force
keep coeff1_reg_*  coeff2_reg_* id

if `j'==1 {
save "$path\Intermediary Data\base_estim_EG_0821_sp95.dta", replace
}
if `j'>1 {
append using "$path\Intermediary Data\base_estim_EG_0821_sp95.dta"
save "$path\Intermediary Data\base_estim_EG_0821_sp95.dta", replace

}

restore
display "End of iteration number" `j'
qui drop if id_select==`j'
}
}

